package com.hg.imajax.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.hg.imajax.bo.MessageBo;
import com.hg.imajax.util.DataSourceUtil;

//聊天消息DAO类
public class MessageDao {

	//根据条查询message信息
	public List<MessageBo> queryList(MessageBo message){
		
		StringBuffer sql = new StringBuffer("select * from tb_message where 1=1");
		if(message!=null&&message.getUser_name()!=null&&!"".equals(message.getUser_name())){
			sql.append(" and user_name = '"+message.getUser_name()+"'");
		}
		if(message!=null&&message.getOpper_name()!=null&&!"".equals(message.getOpper_name())){
			sql.append(" and opper_name = '"+message.getOpper_name()+"'");
		}
		if(message!=null&&message.getMessage_type()!=null&&!"".equals(message.getMessage_type())){
			sql.append(" and message_type = '"+message.getMessage_type()+"'");
		}
		if(message!=null&&message.getIf_read()!=null&&!"".equals(message.getIf_read())){
			sql.append(" and if_read = '"+message.getIf_read()+"'");
		}
		
		Connection con = DataSourceUtil.getConnection();
		Statement statement = null;
		ResultSet resultSet = null;
		List<MessageBo> list = new ArrayList<MessageBo>();
		try {
			statement = con.createStatement();
			resultSet = statement.executeQuery(String.valueOf(sql));
			
			while(resultSet.next()){
				MessageBo bo = new MessageBo();
				bo.setId(resultSet.getInt("id"));
				bo.setCreate_time(resultSet.getString("create_time"));
				bo.setIf_read(resultSet.getString("if_read"));
				bo.setMessage_info(resultSet.getString("message_info"));
				bo.setMessage_type(resultSet.getString("message_type"));
				bo.setOpper_name(resultSet.getString("opper_name"));
				bo.setUser_name(resultSet.getString("user_name"));
				list.add(bo);
			}
			
		} catch (SQLException e) {
			System.out.print("数据库连接异常");
		} finally{
			DataSourceUtil.realseSource(con, statement, resultSet);
		}
		
		
		return list;
	}
	
	//消息插入
	public void insertMessage(MessageBo bo){
		String sql = "insert into tb_message(user_name,message_type,message_info,opper_name,if_read,create_time)";
	    sql+=" value('"+bo.getUser_name()+"','"+bo.getMessage_type()+"','"+bo.getMessage_info()+"','"+bo.getOpper_name()+"','"+bo.getIf_read()+"',now())";
	
	    Connection con = DataSourceUtil.getConnection();
	    Statement statement = null;
	    try {
			 statement = con.createStatement();
			 statement.executeUpdate(sql);
		} catch (SQLException e) {
			System.out.print("数据库连接异常");
		} finally {
			DataSourceUtil.realseSource(con, statement);
		}  
	}
	
	//用户咨询消息绑定客服操作人员
	public int updateMessageOperName(String user_name,String opper_name){
		String sql = "update tb_message set opper_name ='"+opper_name+"' where user_name='"+user_name+"' and opper_name =''";
		Connection con = DataSourceUtil.getConnection();
	    Statement statement = null;
	    int i = 0;
	    try {
			 statement = con.createStatement();
			i = statement.executeUpdate(sql);
		} catch (SQLException e) {
			System.out.print("数据库连接异常");
		} finally {
			DataSourceUtil.realseSource(con, statement);
		} 
	    return i;
	}
	
	//根据ID，更新消息记录是否已读
	public void updateMessageIfRead(Integer id,String if_read){
		String sql = "update tb_message set if_read ='"+if_read+"' where id="+id;
		Connection con = DataSourceUtil.getConnection();
	    Statement statement = null;
	    try {
			 statement = con.createStatement();
			 statement.executeUpdate(sql);
		} catch (SQLException e) {
			System.out.print("数据库连接异常");
		} finally {
			DataSourceUtil.realseSource(con, statement);
		}  
	}
	
}
